package com.example.meitao.fragment;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.meitao.R;
import com.example.meitao.ui.LoginActivity;
import com.example.meitao.ui.OrderActivity;
import com.example.meitao.ui.PrivateActivity;
import com.example.meitao.ui.SignAvtivity;
import com.example.meitao.view.MyMessage;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment implements View.OnClickListener {

    private MyMessage myMessage_order;
    private MyMessage myMessage_garbage;
    //签到
    private ImageView iv_sign;
    //点击登录
    private  ImageView iv_load;
    //保存数据登录记录
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private Boolean isLogin=false ;
    private RadioGroup mRadioGroup;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view =inflater.inflate(R.layout.fragment_mine, container, false);
        initView(view);
        setListener();
        return view;

    }

    private void setListener() {
        myMessage_order.setOnClickListener(this);
        myMessage_garbage.setOnClickListener(this);
        //签到的点击事件
        iv_sign.setOnClickListener(this);
        //登录的点击事件
        iv_load.setOnClickListener(this);
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {

                switch (i) {
                    case R.id.rb_mine_waitspend :
                        Intent intentspend =new Intent(getActivity(),OrderActivity.class);
                      //  intentspend.putExtra("ischecked","1");
                        startActivity(intentspend);
                        break;
                    case R.id.rb_mine_waitsend:
                        Intent intentsend =new Intent(getActivity(),OrderActivity.class);
                      //  intentsend.putExtra("ischecked","2");
                        startActivity(intentsend);
                        break;

                    case R.id.rb_mine_wait_receive:
                        Intent intentreceive =new Intent(getActivity(),OrderActivity.class);
                      //  intentreceive.putExtra("ischecked","3");
                        startActivity(intentreceive);
                        break;
                }

            }
        });

    }

    private void initView(View view) {
        myMessage_order =(MyMessage) view.findViewById(R.id.view_mine_message);
        myMessage_garbage = (MyMessage) view.findViewById(R.id.view_mine_garbage);

        iv_sign = (ImageView) view.findViewById(R.id.iv_mine_sign);
        iv_load = (ImageView) view.findViewById(R.id.iv_mine_icon);

        //初始化SharedPreferences
            sp =getActivity().getPreferences(Context.MODE_PRIVATE);
           // 创建编辑器对象
            editor =sp.edit();

        mRadioGroup = (RadioGroup) view.findViewById(R.id.rg_mine_test);

    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_mine_sign :
                if(isLogin){
                    startActivity(new Intent(getActivity(),SignAvtivity.class));
                    Toast.makeText(getActivity(), "点击了我的签到", Toast.LENGTH_SHORT).show();
                }else{
                    isLogin=true;
                    Intent intent_sign =new Intent(getActivity(), LoginActivity.class);
                   intent_sign.putExtra("sign","sign");
                    startActivity(intent_sign);

            }
                break;
            case R.id.iv_mine_icon:
                if(isLogin){

                    startActivity(new Intent(getActivity(), PrivateActivity.class));

                    Toast.makeText(getActivity(), "点击了登录", Toast.LENGTH_SHORT).show();
                }else {
                    isLogin =true;
                    Intent intent =new Intent(getActivity(), LoginActivity.class);
                    intent.putExtra("login","login");
                    startActivity(intent);

                }
                    break;
            case R.id.view_mine_message:
                Toast.makeText(getActivity(), "点击了我的订单", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(getActivity(),OrderActivity.class));
                break;
            case R.id.view_mine_garbage:
                Toast.makeText(getActivity(), "点击了清除缓存", Toast.LENGTH_SHORT).show();
                break;

        }
    }
}
